package com.onpositive.gamemaker.editor.dialogs;

import java.util.Arrays;
import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;

import com.onpositive.gamemaker.editor.GameEditorConstants;
import com.onpositive.gamemaker.editor.prefs.GameCreatorPrefs;
import com.onpositive.gamemaker.spriteeditor.SpriteDescriptor;

public class SpriteEditDialog2 extends Dialog
{

	protected static final int MAX_SPINNER = 10000;
	protected Button btnBrowse;
	protected Combo fileNameCombo;
	protected Combo typeCombo;
	protected Combo categoryCombo;
	protected Spinner xSpinner;
	protected Spinner ySpinner;
	protected Spinner wSpinner;
	protected Spinner hSpinner;
	
	protected SpriteDescriptor descriptor;
	protected String category;
	protected final String[] categories;
	
	protected SelectionListener browseListener = new SelectionListener()
	{
		
		@Override
		public void widgetSelected(SelectionEvent e)
		{
			FileDialog dialog = new FileDialog(getShell());
	        dialog.setText("Open");
	        String[] filterExt = { "*.jpg,*.png", "*.png", ".gif", "*.*" };
	        dialog.setFilterExtensions(filterExt);
	        dialog.setFilterPath(path);
	        String selected = dialog.open();
	        if (selected != null)
	        {
	        	fileNameCombo.setText(selected);
	        }
		}
		
		@Override
		public void widgetDefaultSelected(SelectionEvent e)
		{
			//Do nothing
		}
	};
	private String path;
	private String initialFileName;

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public SpriteEditDialog2(Shell parentShell, String[] categories)
	{
		super(parentShell);
		this.categories = categories;
		Arrays.sort(this.categories);
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent)
	{
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gl_container = new GridLayout(3, false);
		gl_container.marginHeight = 0;
		gl_container.horizontalSpacing = 0;
		container.setLayout(gl_container);
		
		Label lblSprite = new Label(container, SWT.NONE);
		GridData gd_lblSprite = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_lblSprite.widthHint = 60;
		lblSprite.setLayoutData(gd_lblSprite);
		lblSprite.setText("Sprite");
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		
		Label lblFileName = new Label(container, SWT.NONE);
		lblFileName.setText("File name");
		
		fileNameCombo = new Combo(container, SWT.NONE);
		GridData gd_fileNameCombo = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_fileNameCombo.widthHint = 151;
		fileNameCombo.setLayoutData(gd_fileNameCombo);
		
		btnBrowse = new Button(container, SWT.NONE);
		btnBrowse.setText("Browse...");
		btnBrowse.addSelectionListener(browseListener);
		
		Label lblType = new Label(container, SWT.NONE);
		lblType.setText("Type");
		
		typeCombo = new Combo(container, SWT.NONE);
		typeCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(container, SWT.NONE);
		
		Label lblBounds = new Label(container, SWT.NONE);
		lblBounds.setText("Bounds");
		
		Composite composite = new Composite(container, SWT.NONE);
		GridLayout gl_composite = new GridLayout(8, false);
		gl_composite.marginHeight = 0;
		composite.setLayout(gl_composite);
		GridData gd_composite = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_composite.widthHint = 306;
		gd_composite.heightHint = 24;
		composite.setLayoutData(gd_composite);
		
		Label lblX = new Label(composite, SWT.NONE);
		lblX.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblX.setText("x");
		
		xSpinner = new Spinner(composite, SWT.BORDER);
		GridData gd_xSpinner = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 0);
		gd_xSpinner.widthHint = 29;
		xSpinner.setLayoutData(gd_xSpinner);
		xSpinner.setMaximum(10000);
		
		Label lblY = new Label(composite, SWT.NONE);
		lblY.setText("y");
		
		ySpinner = new Spinner(composite, SWT.BORDER);
		//ySpinner.setMaximum(10);
		GridData gd_ySpinner = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_ySpinner.widthHint = 19;
		ySpinner.setLayoutData(gd_ySpinner);
		ySpinner.setMaximum(10000);
		
		Label lblWidth = new Label(composite, SWT.NONE);
		lblWidth.setText("width");
		
		wSpinner = new Spinner(composite, SWT.BORDER);
		wSpinner.setMaximum(10000);
		
		Label lblH = new Label(composite, SWT.NONE);
		lblH.setText("height");
		
		hSpinner = new Spinner(composite, SWT.BORDER);
		hSpinner.setMaximum(MAX_SPINNER);
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		new Label(container, SWT.NONE);
		
		Label lblCategory = new Label(container, SWT.NONE);
		lblCategory.setText("Category");
		
		categoryCombo = new Combo(container, SWT.NONE);
		categoryCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Button btnCreate = new Button(container, SWT.NONE);
		btnCreate.setText("Create...");
		
		setValues();

		return container;
	}

	protected void setValues()
	{
		String lastPath = GameCreatorPrefs.getLastPath();
		if (lastPath.isEmpty())
			lastPath = initialFileName;
		List<String> savedImagePaths = GameCreatorPrefs.getSavedImagePaths();
		if (!savedImagePaths.contains(initialFileName))
			savedImagePaths.add(initialFileName);		
		fileNameCombo.setItems(savedImagePaths.toArray(new String[0]));
		fileNameCombo.select(savedImagePaths.indexOf(lastPath));
		xSpinner.setSelection(0);
		ySpinner.setSelection(0);
		hSpinner.setSelection(GameCreatorPrefs.getDefaultSpriteHeight());
		wSpinner.setSelection(GameCreatorPrefs.getDefaultSpriteWidth());
		typeCombo.setItems(GameEditorConstants.SPRITE_TYPES);
		typeCombo.select(0);
		categoryCombo.setItems(categories);
		if (descriptor != null)
		{
			fileNameCombo.setText(descriptor.getFileName());
			typeCombo.select(GameEditorConstants.getTypeIndex(descriptor.getType()));
			xSpinner.setSelection(descriptor.getX());
			ySpinner.setSelection(descriptor.getY());
			wSpinner.setSelection(descriptor.getWidth());
			hSpinner.setSelection(descriptor.getHeight());
		}
		if (category != null && !category.isEmpty())
			categoryCombo.select(getCategoryIndex(category));
		
	}
	
	protected int getCategoryIndex(String category)
	{
		for (int i = 0; i < categories.length; i++)
		{
			if (category.equalsIgnoreCase(categories[i]))
				return i;
		}
		return -1;
	}

	@Override
	protected void okPressed()
	{
		if (fileNameCombo.getText().trim().isEmpty())
			return;
		if (wSpinner.getSelection() == 0)
			return;
		if (hSpinner.getSelection() == 0)
			return;
		if (descriptor == null)
		{
			descriptor = new SpriteDescriptor(typeCombo.getText(), fileNameCombo.getText(),xSpinner.getSelection(),ySpinner.getSelection(),wSpinner.getSelection(),hSpinner.getSelection());
		}
		category = categoryCombo.getText();
		GameCreatorPrefs.addSavedImagePath(fileNameCombo.getText());
		GameCreatorPrefs.setDefaultSpriteWidth(wSpinner.getSelection());
		GameCreatorPrefs.setDefaultSpriteHeight(hSpinner.getSelection());
		super.okPressed();
	}
	
	@Override
	protected void cancelPressed()
	{
		descriptor = null;
		super.cancelPressed();
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent)
	{
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize()
	{
		return new Point(450, 230);
	}

	/**
	 * @return the descriptor
	 */
	public SpriteDescriptor getDescriptor()
	{
		return descriptor;
	}

	/**
	 * @param descriptor the descriptor to set
	 */
	public void setDescriptor(SpriteDescriptor descriptor)
	{
		this.descriptor = descriptor;
	}

	/**
	 * @return the category
	 */
	public String getCategory()
	{
		return category;
	}

	/**
	 * @param category the category to set
	 */
	public void setCategory(String category)
	{
		this.category = category;
	}

	public void setFilterPath(String path)
	{
		this.path = path;
	}

	public void setInitialFileName(String initialFileName)
	{
		if (initialFileName == null)
			initialFileName = "";
		this.initialFileName = initialFileName;
	}

}
